import moment from "moment"
/***
 * @param {startTime} 开始时间 "2021-07-01 00:00:00"
 * @param {endTime}   结束时间 "2021-07-02 00:00:00"
 * @param {splitMinNum}    分割时间，单位为分钟  
 * @return {object} 
 */
export function FilterTimeList(startTime, endTime, splitMinNum) {
    function removeDuplicates(arr) {
        if (arr.length === 0) {
            return []
        }
        let fast = 1,
            slow = 1;
        while (fast < arr.length) {
            if (arr[fast] !== arr[fast - 1]) {
                arr[slow] = arr[fast];
                ++slow;
            }
            ++fast;
        }
        return arr.slice(0, slow)
    }

    let startSeconds = new Date(startTime).getTime() / 1000,
        endSeconds = new Date(endTime).getTime() / 1000,
        day = (endSeconds - startSeconds) / (60 * 60 * 24),
        timeList = [],
        result = [];
    let hourSplitMin = 60 / splitMinNum;
    for (let i = 0; i < day * 24 * hourSplitMin; i++) {
        timeList.push(
            moment(new Date((startSeconds + i * 60 * splitMinNum) * 1000)).format(
                "YYYY-MM-DD HH:mm:ss"
            )
        );
    }
    let days = [],
        hours = [];
    for (let i = 0; i < timeList.length; i++) {
        days.push(timeList[i].split(" ")[0]);
        hours.push(timeList[i].split(":")[0]);
    }
    let time = {
        days: removeDuplicates(days),
        hours: removeDuplicates(hours),
    };
    // 按照天，时进行分类
    function getList(time) {
        let list = [];
        for (let i = 0; i < time.days.length; i++) {
            let daysItem = {
                name: time.days[i],
                hoursList: [],
            };
            for (let j = 0; j < time.hours.length; j++) {
                if (time.hours[j].split(" ")[0] == daysItem.name) {
                    let hoursItem = {
                        name: time.hours[j],
                        minList: [],
                    };
                    for (let k = 0; k < timeList.length; k++) {
                        if (timeList[k].split(":")[0] == hoursItem.name) {
                            hoursItem.minList.push(timeList[k]);
                        }
                    }
                    daysItem.hoursList.push(hoursItem);
                }
            }
            list.push(daysItem);
        }
        return list;
    }

    return getList(time);
}
/***
 * @desc 获取元素的索引位置
 * @param {time} 开始时间 "2021-07-01 00:00:00"
 * @return {object} 
 */
export function GetEleIndex(nowTime, eleCollection) {
    let d, h, min;
    for (let i = 0; i < eleCollection.length; i++) {
        if (eleCollection[i].name == nowTime.split(" ")[0]) {
            d = i;
            for (let j = 0; j < eleCollection[i].hoursList.length; j++) {
                if (eleCollection[i].hoursList[j].name == nowTime.split(":")[0]) {
                    h = j;
                    for (let m = 0; m < eleCollection[i].hoursList[j].minList.length; m++) {
                        if (eleCollection[i].hoursList[j].minList[m] == nowTime) {
                            min = m;
                        }
                    }
                }
            }
        }
    }
    return {
        d,
        h,
        min
    }
}

/***
 * @description 设置默认值
 * @param  {any}targetVal
 * @param  {any}defaultVal
 * @return {any}
 */
export function DefaultValue(targetVal, defaultVal) {
    if (typeof targetVal == "undefined" || targetVal == null) {
        return defaultVal
    }
    return targetVal
}
/***
 * @description 判断是否null或者undefined
 * @param {any}val
 * @return {Bollean}
 */
export function Defined(val) {
    if (typeof val == "undefined" || val == null) {
        return false
    }
    return true
}